package com.societyGames.codeAlong.state.codeExercise
{
import com.societyGames.codeAlong.codeExercise.Compiler;
import com.societyGames.codeAlong.state.ChangeStateEvent;
import com.societyGames.codeAlong.state.IState;
import com.societyGames.codeAlong.state.StateEnum;
import com.societyGames.codeAlong.state.codeExercise.model.EditorModel;
import com.societyGames.codeAlong.state.codeExercise.model.ExceptionsData;
import com.societyGames.codeAlong.state.codeExercise.model.ModuleData;
import com.societyGames.codeAlong.state.codeExercise.model.SharedData;
import com.societyGames.codeAlong.state.codeExercise.model.SyntaxErrorsData;
import com.societyGames.codeAlong.state.codeExercise.view.EditorCloseEnum;
import com.societyGames.codeAlong.state.codeExercise.view.EditorCloseEvent;
import com.societyGames.codeAlong.state.codeExercise.view.EditorView;
import com.societyGames.codeAlong.user.PersistentUserData;
import com.societyGames.codeAlong.user.UserDataError;
import com.societyGames.system.EventObject;
import com.societyGames.system.FileCache;

import flash.display.DisplayObject;
import flash.events.EventDispatcher;

public class CodeExerciseState extends EventDispatcher implements IState
{
  private var _changeStateEventObject:EventObject;
  private var _userData:PersistentUserData;
  private var _editorModel:EditorModel;
  private var _editorView:EditorView;

  public function CodeExerciseState(userData:PersistentUserData, moduleData:ModuleData, moduleDatas:Vector.<ModuleData>, sharedData:SharedData, syntaxErrorsData:SyntaxErrorsData, exceptionsData:ExceptionsData, compiler:Compiler, fileCache:FileCache)
  {
    this._changeStateEventObject = new EventObject(this, "CHANGE_STATE");
    this._userData = userData;

    if (this._userData.userDataError != UserDataError.EMPTY) //if it's the first time playing don't try saving.
    {
      this._userData.currentExerciseId = moduleData.id;
      this._userData.save();
    }

    this._editorView = new EditorView(this._userData, moduleDatas);
    this._editorView.closeEventObject.addEventListener(editorCompleteHandler);

    this._editorModel = new EditorModel(userData, moduleData, syntaxErrorsData, exceptionsData, compiler, fileCache, sharedData);
    this._editorView.setEditorModel(this._editorModel);
  }

  //IState-->
  public function get displayObject():DisplayObject //The display object to be displayed.
  {
    return this._editorView;
  }

  public function exit():void
  {
    if (this._editorModel.testCaseController.selectedTestCase.canStop)
    {
      this._editorModel.testCaseController.stop();
    }
  }

  public function get changeStateEventObject():EventObject //ChangeStateEvent
  {
    return this._changeStateEventObject;
  }

  //<--IState

  /*private function editorModelLoadedHandler(event:Event):void
   {
   this._editorView.setEditorModel(this._editorModel);
   }*/

  private function editorCompleteHandler(event:EditorCloseEvent):void
  {
    this._editorView.closeEventObject.removeEventListener(editorCompleteHandler);
    switch (event.editorCloseEnum)
    {
      case EditorCloseEnum.nextExercise:
        this._changeStateEventObject.dispatchEvent(new ChangeStateEvent(this._changeStateEventObject.type, StateEnum.GoToNextExercise, null));
        break;
      case EditorCloseEnum.selectModuleData:
        this._changeStateEventObject.dispatchEvent(new ChangeStateEvent(this._changeStateEventObject.type, StateEnum.GoToExercise, this._editorView.selectedModuleData));
        break;
    }
  }
}
}